# Multiple Linear Regression

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold, GridSearchCV, KFold, \
    cross_validate
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn import preprocessing

import joblib



def test(path, predictionObject, data):
    regressor = None
    testData = None
    if predictionObject == 'ingotRate':
        if path=='/model':
            decisionTreeRegression =joblib.load('.'+path+'/decisionTreeRegressor/ingotRatePrediction.pkl')
            KNNRegression =joblib.load('.'+path+'/KNNRegression/ingotRatePrediction.pkl')
            lassoRegression =joblib.load('.'+path+'/lassoRegression/ingotRatePrediction.pkl')
            MLPRegression =joblib.load('.'+path+'/MLPRegression/ingotRatePrediction.pkl')
            multipleLinearRegression =joblib.load('.'+path+'/multipleLinearRegression/ingotRatePrediction.pkl')
            ridgeRegression =joblib.load('.'+path+'/ridgeRegression/ingotRatePrediction.pkl')
            SVR =joblib.load('.'+path+'/SVR/ingotRatePrediction.pkl')
        elif path=='':
            decisionTreeRegression = joblib.load('..' + path + '/decisionTreeRegressor/ingotRatePrediction.pkl')
            KNNRegression = joblib.load('..' + path + '/KNNRegression/ingotRatePrediction.pkl')
            lassoRegression = joblib.load('..' + path + '/lassoRegression/ingotRatePrediction.pkl')
            MLPRegression = joblib.load('..' + path + '/MLPRegression/ingotRatePrediction.pkl')
            multipleLinearRegression = joblib.load('..' + path + '/multipleLinearRegression/ingotRatePrediction.pkl')
            ridgeRegression = joblib.load('..' + path + '/ridgeRegression/ingotRatePrediction.pkl')
            SVR = joblib.load('..' + path + '/SVR/ingotRatePrediction.pkl')
        testData = pd.DataFrame(data,
                                columns=['WS_MM', 'CS_MM', 'FS_MM', 'Mn_MM', 'CL_SM', 'Out_TE', 'S_EL', 'SN_QM',
                                         'UD_QM', 'NI_QM', 'OE_QM', 'PO_QM', 'C_QM', 'SI_QM'], dtype=float)
    elif predictionObject == 'yieldRate':
        # regressor = joblib.load('./model/decisionTreeRegressor/yieldRatePrediction.pkl')
        # regressor = joblib.load('./yieldRatePrediction.pkl')
        if path=='/model':
            decisionTreeRegression =joblib.load('.'+path+'/decisionTreeRegressor/yieldRatePrediction.pkl')
            KNNRegression =joblib.load('.'+path+'/KNNRegression/yieldRatePrediction.pkl')
            lassoRegression =joblib.load('.'+path+'/lassoRegression/yieldRatePrediction.pkl')
            MLPRegression =joblib.load('.'+path+'/MLPRegression/yieldRatePrediction.pkl')
            multipleLinearRegression =joblib.load('.'+path+'/multipleLinearRegression/yieldRatePrediction.pkl')
            ridgeRegression =joblib.load('.'+path+'/ridgeRegression/yieldRatePrediction.pkl')
            SVR =joblib.load('.'+path+'/SVR/yieldRatePrediction.pkl')
        elif path=='':
            decisionTreeRegression = joblib.load('..' + path + '/decisionTreeRegressor/yieldRatePrediction.pkl')
            KNNRegression = joblib.load('..' + path + '/KNNRegression/yieldRatePrediction.pkl')
            lassoRegression = joblib.load('..' + path + '/lassoRegression/yieldRatePrediction.pkl')
            MLPRegression = joblib.load('..' + path + '/MLPRegression/yieldRatePrediction.pkl')
            multipleLinearRegression = joblib.load('..' + path + '/multipleLinearRegression/yieldRatePrediction.pkl')
            ridgeRegression = joblib.load('..' + path + '/ridgeRegression/yieldRatePrediction.pkl')
            SVR = joblib.load('..' + path + '/SVR/yieldRatePrediction.pkl')
        testData = pd.DataFrame(data,
                                columns=['X31', 'X33', 'X34', 'X35', 'X36'], dtype=float)

    # testData = preprocessing.scale(testData)
    # testData = preprocessing.StandardScaler().fit_transform(testData)
    # print(testData.values[0])
    result_decisionTreeRegression = decisionTreeRegression.predict(testData.values)
    result_KNNRegression = KNNRegression.predict(testData.values)
    result_lassoRegression = lassoRegression.predict(testData.values)
    result_MLPRegression = MLPRegression.predict(testData.values)
    result_multipleLinearRegression = multipleLinearRegression.predict(testData.values)
    result_ridgeRegression = ridgeRegression.predict(testData.values)
    result_SVR = SVR.predict(testData.values)

    # results=result_KNNRegression
    # result = results

    # results=np.vstack([results,result_decisionTreeRegression])
    # results=np.vstack([results,result_KNNRegression])

    results=np.vstack([result_decisionTreeRegression,result_KNNRegression])
    results=np.vstack([results,result_lassoRegression])
    results=np.vstack([results,result_MLPRegression])
    results=np.vstack([results,result_multipleLinearRegression])
    results=np.vstack([results,result_ridgeRegression])
    results=np.vstack([results,result_SVR])

    result=results.mean(axis=0)


    print('result:\n', result)
    return result




if __name__ == '__main__':
    # train('../../data/ingotRate.csv','./ingotRatePrediction.pkl')
    # train('../../data/yieldRate.csv', './yieldRatePrediction.pkl')

    dataPath='../../data/ingotRate.csv'
    # dataPath='../../data/yieldRate.csv'

    dataset = pd.read_csv(dataPath)
    X = dataset.iloc[:, :-1]
    # X = dataset.iloc[:, [0,1,2,3,5,9,10,12]]
    y = dataset.iloc[:, dataset.shape[1] - 1]

    # normalize
    scl = preprocessing.StandardScaler()
    X_scaled = scl.fit_transform(X)

    if dataPath == '../../data/ingotRate.csv':
        X_train, X_test, y_train, y_test = train_test_split(X_scaled, y.values, test_size=0.2, random_state=3)
        y_pred=test('','ingotRate', X_test)
    elif dataPath == '../../data/yieldRate.csv':
        X_train, X_test, y_train, y_test = train_test_split(X_scaled, y.values, test_size=0.2, random_state=0)
        y_pred=test('','yieldRate', X_test)

    print('\ntest: ')
    MSE = mean_squared_error(y_test, y_pred)
    MAE = mean_absolute_error(y_test, y_pred)
    R2_Score = r2_score(y_test, y_pred)
    print("MSE: ", MSE)
    print("MAE: ", MAE)
    print("R2_Score: ", R2_Score)











